package duppentity.dto.bu;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import constants.BuDefault;
import utils.JSONObjectUtils;
import utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class BuDtoUtil {

    public static StationDto getStationDto(JsonObject data) {
        StationDto stationDto =new StationDto();
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    JSONObject listOne = JSONObjectUtils.get(list, 0);
                    stationDto = convertStationDto(listOne,data);
                }
            }
        }
        return stationDto;
    }

    private static StationDto convertStationDto(JSONObject listOne,JsonObject data){
        StationDto stationDto = new StationDto();
        stationDto.id = listOne.getString("id");
        stationDto.name = listOne.getString("name");

        stationDto.deviceId = listOne.getLongValue("deviceId");
        if (stationDto.deviceId>0){
            JSONObject buDeviceWash = JSONObjectUtils.get(data, "bu_device_wash");
            stationDto.deviceNo = JSONObjectUtils.getString(buDeviceWash,listOne.getString("deviceId"),"deviceBoxId");
        }
        stationDto.gateType = listOne.getIntValue("gateType");
        JSONObject buDeviceGate = JSONObjectUtils.get(data, "bu_device_gate");
        stationDto.enterGateId = listOne.getLongValue("enterGateId");
        if (stationDto.enterGateId>0){
            stationDto.enterRomId = JSONObjectUtils.getString(buDeviceGate,listOne.getString("enterGateId"),"romid");
        }
        stationDto.exitGateId = listOne.getLongValue("exitGateId");
        if (stationDto.exitGateId>0){
            stationDto.exitRomId = JSONObjectUtils.getString(buDeviceGate,listOne.getString("exitGateId"),"romid");
        }

        stationDto.name = listOne.getString("name");
        stationDto.price = Utils.getStringPrice(listOne.getInteger("price"));
        stationDto.discountRate = Utils.getStringPrice(listOne.getInteger("discountRate"),10);
        stationDto.lng = listOne.getDoubleValue("lng");
        stationDto.lat = listOne.getDoubleValue("lat");
        stationDto.address = listOne.getString("address");
        stationDto.openTimeDesc = listOne.getString("openTimeDesc");
        stationDto.openStatus = listOne.getIntValue("openStatus");
        stationDto.serviceStatus = listOne.getIntValue("serviceStatus");
        stationDto.successCount = listOne.getIntValue("successCount");
        stationDto.failCount = listOne.getIntValue("failCount");
        stationDto.income = Utils.getStringPrice(listOne.getIntValue("income"));
        stationDto.openStatusName = BuDefault.getResultName(stationDto.openStatus,BuDefault.nameType.openStatus);
        stationDto.serviceStatusName = BuDefault.getResultName(stationDto.serviceStatus,BuDefault.nameType.serviceStatus);
        stationDto.remark = listOne.getString("remark");
        stationDto.createTime = listOne.getLong("createTime");
        stationDto.modifyTime = listOne.getLong("modifyTime");
        stationDto.deviceStatus = listOne.getIntValue("deviceStatus");
        stationDto.washDeviceStatus = listOne.getIntValue("washDeviceStatus");
        stationDto.enterGateStatus = listOne.getIntValue("enterGateStatus");
        stationDto.exitGateStatus = listOne.getIntValue("exitGateStatus");
        return  stationDto;
    }



    public static List<StationDto> getStationDtoList(JsonObject data){
        List<StationDto> sysStationDtoList = null;
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        sysStationDtoList = new ArrayList<>();
                        for (Integer i=0;i<list.size();i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            StationDto adDto = convertStationDto(listOne,data);
                            sysStationDtoList.add(adDto);
                        }
                    }
                }
            }
        }
        return  sysStationDtoList;
    }


    public static List<CouponDto> getCouponDtoList(JsonObject data){
        List<CouponDto> couponDtoList = null;
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        couponDtoList = new ArrayList<>();
                        for (Integer i=0;i<list.size();i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            CouponDto adDto = convertCouponDto(listOne,data);
                            couponDtoList.add(adDto);
                        }
                    }
                }
            }
        }
        return  couponDtoList;
    }


    public static CouponDto getCouponDto(JsonObject data) {
        CouponDto couponDto =new CouponDto();
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    JSONObject listOne = JSONObjectUtils.get(list, 0);
                    couponDto = convertCouponDto(listOne,data);
                }
            }
        }
        return couponDto;
    }

    private static CouponDto convertCouponDto(JSONObject listOne,JsonObject data) {
        CouponDto couponDto = new CouponDto();
        couponDto.id = listOne.getString("id");
        couponDto.name = listOne.getString("name");
        couponDto.type = listOne.getIntValue("type");
        couponDto.typeName = BuDefault.getResultName(couponDto.type,BuDefault.nameType.couponType);
        couponDto.val =Utils.getStringPrice(listOne.getIntValue("val"));
        couponDto.useType =listOne.getIntValue("use");
        couponDto.useTypeName = BuDefault.getResultName(couponDto.useType,BuDefault.nameType.couponUse);
        couponDto.limitNum =listOne.getIntValue("limit");
        couponDto.invalid =listOne.getIntValue("invalid");
        couponDto.validType =listOne.getIntValue("validType");
        if (couponDto.validType==1) {
            couponDto.validDay = listOne.getIntValue("validDay");
        }else{
            couponDto.startTime = listOne.getLongValue("startTime");
            couponDto.endTime = listOne.getLongValue("endTime");
        }
        couponDto.dispatchType =listOne.getIntValue("dispatchType");
        couponDto.dispatchTypeName =BuDefault.getResultName(couponDto.dispatchType,BuDefault.nameType.dispatchType);
        couponDto.totalCount =listOne.getIntValue("totalCount");
        couponDto.remainCount =listOne.getIntValue("remainCount");
        couponDto.dispatchCount =listOne.getIntValue("dispatchCount");
        couponDto.donateCount =listOne.getIntValue("donateCount");
        couponDto.usedCount =listOne.getIntValue("usedCount");
        couponDto.couponStatus = listOne.getIntValue("status");
        couponDto.couponStatusName =BuDefault.getResultName(couponDto.couponStatus,BuDefault.nameType.status);
        couponDto.createTime =listOne.getLongValue("createTime");
        couponDto.modifyTime =listOne.getLongValue("modifyTime");
        couponDto.createBy =listOne.getLongValue("createBy");
        if (couponDto.createBy>0) {
            JSONObject sysAdmin = JSONObjectUtils.get(data, "sys_admin");
            couponDto.createByName = JSONObjectUtils.getString(sysAdmin, listOne.getString("createBy"), "name");
        }
        couponDto.modifyBy =listOne.getLongValue("modifyBy");
        return  couponDto;
    }


    public static List<CouponUserDto> getCouponUserDtoList(JsonObject data){
        List<CouponUserDto> couponUserDtoList = null;
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        couponUserDtoList = new ArrayList<>();
                        for (Integer i=0;i<list.size();i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            CouponUserDto adDto = convertCouponUserDto(listOne,data);
                            couponUserDtoList.add(adDto);
                        }
                    }
                }
            }
        }
        return  couponUserDtoList;
    }


    private static CouponUserDto convertCouponUserDto(JSONObject listOne,JsonObject data) {
        CouponUserDto couponUserDto = new CouponUserDto();
        couponUserDto.id = listOne.getString("id");
        couponUserDto.couponId = listOne.getLongValue("couponId");
        couponUserDto.couponCodeId = listOne.getLongValue("couponCodeId");

        couponUserDto.couponCodeId = listOne.getLongValue("couponCodeId");
        if (couponUserDto.couponCodeId>0){
            JSONObject buCouponCode = JSONObjectUtils.get(data, "bu_coupon_code");
            couponUserDto.couponCodeName = JSONObjectUtils.getString(buCouponCode,listOne.getString("couponCodeId"),"couponCode");
           Integer status= JSONObjectUtils.getInteger(buCouponCode,listOne.getString("couponCodeId"),"status");
            couponUserDto.couponStatusName = BuDefault.getResultName(status,BuDefault.nameType.couponStatus);
        }

        couponUserDto.userId = listOne.getLongValue("userId");
        couponUserDto.dispatchUserId = listOne.getLongValue("dispatchUserId");
        JSONObject userInfo = JSONObjectUtils.get(data, "user_info");
        JSONObject sysAdmin = JSONObjectUtils.get(data, "sys_admin");
        JSONObject userThirdUnionId = JSONObjectUtils.get(data, "user_third_union_id");
        if (couponUserDto.userId>0) {
            JSONObject userInfoDto = userInfo.getJSONObject(listOne.getString("userId"));
            couponUserDto.mobile = userInfoDto.getString("mobile").replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
            JSONArray userThirdUnionIds = userInfoDto.getJSONArray("userThirdUnionIds");
            if (userThirdUnionIds != null && userThirdUnionIds.size() > 0) {
                couponUserDto.userName = JSONObjectUtils.getString(userThirdUnionId, userThirdUnionIds.get(0).toString(), "nickname");
            }
        }
        if (couponUserDto.dispatchUserId>0) {
            JSONObject userInfoDto = userInfo.getJSONObject(listOne.getString("dispatchUserId"));
            if (userInfoDto!=null && userInfoDto.containsKey("id")) {
                JSONArray userThirdUnionIds = userInfoDto.getJSONArray("userThirdUnionIds");
                if (userThirdUnionIds != null && userThirdUnionIds.size() > 0) {
                    couponUserDto.dispatchUserName = JSONObjectUtils.getString(userThirdUnionId, userThirdUnionIds.get(0).toString(), "nickname");
                }
            }else{
                couponUserDto.dispatchUserName = JSONObjectUtils.getString(sysAdmin, listOne.getString("dispatchUserId"), "name");
            }
        }
        couponUserDto.getWay =listOne.getIntValue("getWay");
        couponUserDto.getWayName = BuDefault.getResultName(couponUserDto.getWay,BuDefault.nameType.getWay);
        couponUserDto.donateTime =listOne.getLongValue("donateTime");
        couponUserDto.useTime =listOne.getLongValue("useTime");
        couponUserDto.orderId =listOne.getLongValue("orderId");
        if (couponUserDto.orderId>0) {
            JSONObject userOrder = JSONObjectUtils.get(data, "user_order");
            couponUserDto.orderNo = JSONObjectUtils.getString(userOrder, listOne.getString("orderId"), "orderNo");
        }

        couponUserDto.createTime =listOne.getLongValue("createTime");
        couponUserDto.modifyTime =listOne.getLongValue("modifyTime");
        couponUserDto.createBy =listOne.getLongValue("createBy");
        couponUserDto.modifyBy =listOne.getLongValue("modifyBy");
        return  couponUserDto;
    }






}
